
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using System.Linq.Expressions;
using Apps.IDAL.PUB;
using Apps.Models.PUB;
using Apps.IBLL.PUB;
namespace Apps.BLL.PUB
{
	public partial class PUB_MANUFACTURERBLL: Virtual_PUB_MANUFACTURERBLL,IPUB_MANUFACTURERBLL
	{
        

	}
	public class Virtual_PUB_MANUFACTURERBLL
	{
        [Dependency]
        public IPUB_MANUFACTURERRepository m_Rep { get; set; }

		public virtual List<PUB_MANUFACTURERModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<PUB_MANUFACTURER> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
								a=>  a.FACTURER_NAME.Contains(queryStr)
								|| a.REMARKS.Contains(queryStr)
								);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
		 
		public virtual List<PUB_MANUFACTURERModel> GetPagerList(ref GridPager pager, Expression<Func<PUB_MANUFACTURER, bool>> whereLambda )
        {

            IQueryable<PUB_MANUFACTURER> queryData = m_Rep.GetList(whereLambda); 
             
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

		public virtual List<PUB_MANUFACTURERModel> GetList(Expression<Func<PUB_MANUFACTURER, bool>> whereLambda )
        {

            IQueryable<PUB_MANUFACTURER> queryData = m_Rep.GetList(whereLambda); 
       
            return CreateModelList(ref queryData);
        }

		public virtual List<PUB_MANUFACTURERModel> GetListByParentId(ref GridPager pager, string queryStr,object parentId)
        {
			return new List<PUB_MANUFACTURERModel>();
		}

        public virtual List<PUB_MANUFACTURERModel> CreateModelList(ref IQueryable<PUB_MANUFACTURER> queryData)
        {

            List<PUB_MANUFACTURERModel> modelList = (from r in queryData
                                              select new PUB_MANUFACTURERModel
                                              {
													FACTURER_ID = r.FACTURER_ID,
													FACTURER_NAME = r.FACTURER_NAME,
													REMARKS = r.REMARKS,
													ENUM_CODE = r.ENUM_CODE,
          
                                              }).ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, PUB_MANUFACTURERModel model)
        {
            try
            {
                PUB_MANUFACTURER entity = m_Rep.GetById(model.FACTURER_ID);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new PUB_MANUFACTURER();
               				entity.FACTURER_ID = model.FACTURER_ID;
				entity.FACTURER_NAME = model.FACTURER_NAME;
				entity.REMARKS = model.REMARKS;
				entity.ENUM_CODE = model.ENUM_CODE;
  

                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
		 public virtual bool Deletes(ref ValidationErrors errors,Expression<Func<PUB_MANUFACTURER, bool>> whereLambda)
         {
		        try
				{
					using (DBContainer db = new DBContainer())
					{
				
							foreach (var entity in db.PUB_MANUFACTURER.Where(whereLambda))
							{
								db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
							}

							if(db.SaveChanges()>0)
							{
								return true;
							}
							else
							{
								return false;
							}
			
					}
				}
				catch (Exception ex)
				{
					errors.Add(ex.Message);
					ExceptionHander.WriteException(ex);
					return false;
				}
        }

         public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
       

        public virtual bool Edit(ref ValidationErrors errors, PUB_MANUFACTURERModel model)
        {
            try
            {
                PUB_MANUFACTURER entity = m_Rep.GetById(model.FACTURER_ID);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                              				entity.FACTURER_ID = model.FACTURER_ID;
				entity.FACTURER_NAME = model.FACTURER_NAME;
				entity.REMARKS = model.REMARKS;
				entity.ENUM_CODE = model.ENUM_CODE;
 


                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

      

        public virtual PUB_MANUFACTURERModel GetById(object id)
        {
            if (IsExists(id))
            {
                PUB_MANUFACTURER entity = m_Rep.GetById(id);
                PUB_MANUFACTURERModel model = new PUB_MANUFACTURERModel();
                              				model.FACTURER_ID = entity.FACTURER_ID;
				model.FACTURER_NAME = entity.FACTURER_NAME;
				model.REMARKS = entity.REMARKS;
				model.ENUM_CODE = entity.ENUM_CODE;
 
                return model;
            }
            else
            {
                return null;
            }
        }

		public virtual PUB_MANUFACTURERModel GetItem(Expression<Func<PUB_MANUFACTURER, bool>> whereLambda)
        {
 
                PUB_MANUFACTURER entity = m_Rep.GetItem(whereLambda);
                if (entity == null) return null;

                PUB_MANUFACTURERModel model = new PUB_MANUFACTURERModel();
                              				model.FACTURER_ID = entity.FACTURER_ID;
				model.FACTURER_NAME = entity.FACTURER_NAME;
				model.REMARKS = entity.REMARKS;
				model.ENUM_CODE = entity.ENUM_CODE;
 
                return model; 
        }


		 /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<PUB_MANUFACTURERModel> list,ref ValidationErrors errors )
        {
          
            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
			 				 excelFile.AddMapping<PUB_MANUFACTURERModel>(x => x.FACTURER_ID, "FACTURER_ID");
				 excelFile.AddMapping<PUB_MANUFACTURERModel>(x => x.FACTURER_NAME, "FACTURER_NAME");
				 excelFile.AddMapping<PUB_MANUFACTURERModel>(x => x.REMARKS, "REMARKS");
				 excelFile.AddMapping<PUB_MANUFACTURERModel>(x => x.ENUM_CODE, "ENUM_CODE");
 
            //SheetName
            var excelContent = excelFile.Worksheet<PUB_MANUFACTURERModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new PUB_MANUFACTURERModel();
						 				  entity.FACTURER_ID = row.FACTURER_ID;
				  entity.FACTURER_NAME = row.FACTURER_NAME;
				  entity.REMARKS = row.REMARKS;
				  entity.ENUM_CODE = row.ENUM_CODE;
 
                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<PUB_MANUFACTURERModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        PUB_MANUFACTURER entity = new PUB_MANUFACTURER();
                       						entity.FACTURER_ID = model.FACTURER_ID;
						entity.FACTURER_NAME = model.FACTURER_NAME;
						entity.REMARKS = model.REMARKS;
						entity.ENUM_CODE = model.ENUM_CODE;
 
                        db.PUB_MANUFACTURER.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
		  public void Dispose()
        { 
            
        }

	}
}
